package edu.sugar.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import edu.sugar.bean.KW;
import edu.sugar.service.TrafficService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class SugarController {

    int i = 0;

    @Autowired
    TrafficService trafficService;

    @RequestMapping("/sugar/kw")
    public String kw(Integer date) {

      // 如果是 null, 表示要查的是当天
        if (date == null) {
            //如果有传输日期，就自动输入今天的日期
            date = Integer.valueOf(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }


        List<KW> list = trafficService.kw(date);
        JSONObject result = new JSONObject(true);

        result.put("status",i);
        result.put("msg", "");


        JSONArray data = new JSONArray();

        for (KW kw : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", kw.getKeyword());
            obj.put("value", kw.getScore());
            System.out.println(kw.getScore());
            data.add(obj);
        }

        result.put("data", data);
        return result.toJSONString();

    }
}
